package com.taobao.vsearch;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: caoling
 * Date: 11-3-14
 * Time: 1:47 pm
 * To change this template use File | Settings | File Templates.
 */
public class DataConfig {
    private XMLConfiguration xmlConfiguration;

	public DataConfig(InputStream in) throws ConfigurationException {
		xmlConfiguration = new XMLConfiguration();
		xmlConfiguration.load(in);
	}

	public String docName() {
		return xmlConfiguration.getString("document[@name]");
	}

	public String transformer() {
		return xmlConfiguration.getString("document.entity[@transformer]");
	}

	public Map<String, String> field() {
		Map<String, String> fields = new HashMap<String, String>();
		int size = xmlConfiguration.getList("document.entity.field[@name]").size();

		for (int i = 0; i < size; i++) {
			String column = xmlConfiguration.getString("document.entity.field(" + i + ")[@column]");
			String name = xmlConfiguration.getString("document.entity.field(" + i + ")[@name]");
			fields.put(column, name);
		}
		return fields;
	}
}
